<template>
  <el-dialog
    title="流程回退"
    :visible.sync="dialogVisible"
    width="80%"
    :append-to-body="true"
  >
    <el-table
      :data="tableData"
      style="width: 100%"
      row-key="kid"
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="任务ID"></el-table-column>
      <el-table-column prop="name" label="任务名称"></el-table-column>
      <!-- <el-table-column prop="owner" label="发布者" ></el-table-column> -->
      <el-table-column prop="assignee" label="执行人"></el-table-column>
      <el-table-column
        prop="processInstanceId"
        label="实例ID"
      ></el-table-column>
      <el-table-column
        prop="taskDefinitionKey"
        label="业务key"
        width="320"
      ></el-table-column>
      <el-table-column
        prop="processDefinitionId"
        label="流程定义ID"
      ></el-table-column>
      <el-table-column
        prop="startTime"
        label="发起时间"
        :formatter="timeFormat"
      ></el-table-column>
      <el-table-column
        prop="endTime"
        label="结束时间"
        :formatter="timeFormat"
      ></el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="流程回退"
            placement="top-start"
          >
            <el-button
              v-if="scope.row.id != id"
              type="primary"
              size="small"
              @click="backService(scope.row.id)"
            >
              回退到此节点
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>
<script>
import TreeSelect from "@/components/basic-treeselect/treeselect";
import { activeUrl } from "@/config/env";
const urls = {
  list: activeUrl + "/task/get_list_by_instance",
  back: activeUrl + "/rollBack"
};
export default {
  data() {
    return {
      dialogVisible: false,
      tableData: [],
      defaultCheckedKeys: [],
      form: {
        objectId: null,
        name: null,
        note: null
      },
      id: null,
      rules: {}
    };
  },
  components: {
    TreeSelect
  },
  methods: {
    popoverHide(checkedIds, checkedData) {
      this.form.menus = checkedIds.toString();
    },
    show(processInstanceId,id) {
      console.log(processInstanceId,id)
      this.dialogVisible = true;
      if (this.$refs["closeForm"]) {
        this.$refs["closeForm"].resetFields();
      }
      this.id = id;
      this.fetch(processInstanceId,id);
    },
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          axios.post(urls.save, this.form).then(res => {
            this.$message(res.data.result);
            this.dialogVisible = false;
            this.$emit("refresh");
          });
        }
        return false;
      });
    },
    fetch(processInstanceId,id) {
      console.log(processInstanceId,id)
      axios
        .get(urls.list, { params: { processInstanceId: processInstanceId } })
        .then(res => {
          this.tableData = res.data;
        });
    },
    backService(id) {
      this.$confirm("确认要回退吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        axios
          .get(urls.back, {
            params: { currentTaskId: this.id, backToTaskId: id }
          })
          .then(res => {
            console.log(res);
            this.$message(res.data.result);
            this.dialogVisible = false;
            this.$emit("refresh");
          });
      });
    },
    timeFormat(row, column) {
      // 工作流时间格式化
      return new Date(+new Date(row.createTime) + 8 * 3600 * 1000)
        .toISOString()
        .replace(/T/g, " ")
        .replace(/\.[\d]{3}Z/, "");
    }
  }
};
</script>
